import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
import { removeToken, removeUserInfo, getUserInfo, getToken, setUserInfo } from '../../utils/storage'
import config from '../../config/index'
import { checkExpireVip, getUserDetail } from '../../api/wxuser/index'

Page({
  data: {
    user: {} as WxUser,
    defaultAva: '../../static/images/defult_user.png',
    isLogin: false,
    configBaseUrl: config.BaseUrl + '/',
    panelShow: false,
    opList: [
      {
        id: 1,
        title: '我的个人车源',
        url: 'mychannel'
      },
      {
        id: 2,
        title: '我的新车线索',
        url: 'myclue'
      },
      {
        id: 3,
        title: '我的同行车源',
        url: 'mypeer'
      },
      {
        id: 4,
        title: '我的发布车源',
        url: 'myrelease'
      }
    ]
  },
  async getInfo() {
    const res = await getUserDetail(this.data.user.id);
    if(res.statusCode === config.SUCCESS_CODE) {
      setUserInfo(res.data)
      const user = getUserInfo()
      this.setData({
        user
      })
    } 
  },
  openRules() {
    wx.downloadFile({
      url: config.BaseUrl + '/files/rules.pdf',
      success: (res) => {
        wx.openDocument({
          filePath: res.tempFilePath
        })
      } 
    })
  },
  onShow() {
    if(getToken()) {
      if(this.data.user.id) {
        this.getInfo()
      }
      const user = getUserInfo()
      this.setData({
        user,
        isLogin: true
      })
    }
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
    //检查vip是否过期
    const id = this.data.user.id as string
    checkExpireVip(id).then((res) => {
      if(res.statusCode === config.SUCCESS_CODE) {
        //过期就更新用户信息
        if(res.data) {
          getUserDetail(id).then((r) => {
            if(r.statusCode === config.SUCCESS_CODE) {
              setUserInfo(r.data)
              this.setData({
                user: getUserInfo()
              })
              return
            }
          })
        }
      }
    })
  },
  toGetVip() {
    wx.navigateTo({
      url: '/pages/getVip/getVip'
    })
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  toMeDetail() {
    wx.navigateTo({
      url: '/pages/meDetail/meDetail'
    })
  },
  toOP(e: any) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: `/pages/${url}/${url}`
    })
  },
  toChoose() {
    this.getTabBar().setData({
      show: false
    })
    this.setData({
      panelShow: true
    })
  },
  onClose(e: any) {
    this.setData({
      panelShow: e.detail
    })
    this.getTabBar().setData({
      show: true
    })
  },
  reLaunch() {
    wx.reLaunch({
      url: `/pages/me/me`
    })
  },
  logout() {
    Dialog.confirm({
      title: '退出登录',
      message: '确认退出登录？',
    })
      .then(() => {
        removeToken()
        removeUserInfo()
        wx.showToast({
          title: '退出成功',
          icon: 'success',
          success: () => {
            setTimeout(() => {
              wx.reLaunch({
                url: '/pages/login/login'
              })
            }, 1000)
          }
        })
      })
      .catch(() => {});
  }
})